
package system.base;

import java.io.File;

import framework.core.*;
import system.LoginContext;
import system.data.*;
/*
	Author    :Arrow
	CreateTime:2009-01-05 10:28:05
*/
public class WorldExpSrv extends IHandler
{

	public WorldExpSrv() 
	{
		super();
	}
	private void deleteWorldExp(DBConnection con,SrvContext context) throws Exception
	{
		String[] ids = context.getParameterValues("ids");
		WorldExp obj = null;
		for(int i=0;i<ids.length;i++)
		{
			obj = WorldExp.fetch(con,ids[i]);
			String strPath = context.m_req.getRealPath("/");
	        if (!strPath.endsWith("\\"))
	        {
	            strPath +="\\";
	        }
	        String filePath = strPath + obj.getFilePath();
	        
	        File f2 = new File(filePath);
	        try{
	            if(f2.exists()) f2.delete();
	        }
	        catch(Exception e){
	            LogWriter.error(e.toString());
	        }
	        
			if(obj!=null) obj.delete(con);
		}
	}
	private void saveWorldExp(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String id = context.getString("Id",null);
		WorldExp obj;
		if((id==null)||(id.equals("")))   //add
		{
			obj = WorldExp.fetch();
    	}
    	else                     //update
    	{
    		obj = WorldExp.fetch(con,id);
    	}
		WorldExpPageData data = (WorldExpPageData)PageData.fromForm(context,WorldExpPageData.class);
		context.setValue("WorldExpPageData",data);
		obj.fill(data);
		obj.save(con);
	}
	private void listWorldExp(DBConnection con,SrvContext context, ILoginContext loginContext, String path) throws Exception
	{
		String warehouseCode = loginContext.getLoginWarehouseCode();
		String warehouseJtCode = WorldExp.changeWarehouseJTCode(warehouseCode);
	    WorldExpSearchEvent event = (WorldExpSearchEvent)SearchEvent.fromForm(context,WorldExpSearchEvent.class);
		if("".equals(event.getWarehouseCode().trim()) && !"00".equals(warehouseJtCode)){
		    event.setWarehouseCode(warehouseJtCode);
		}
		context.setSessionValue("WorldExpSearchEvent",event);
		context.setNextUrl(path+"WorldExpList.jsp");
	}
	private void backWorldExp(DBConnection con,SrvContext context,String path) throws Exception
	{
		context.setNextUrl(path+"WorldExpList.jsp");
	}
	
	public void doAction(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String action_code = context.getActionCode();
		String path = "/system/base/";
		
		if(action_code.equalsIgnoreCase("listWorldExp"))
		{
			listWorldExp(con,context, loginContext, path);
			return;
		}
		if(action_code.equalsIgnoreCase("viewWorldExp"))
		{
			String id = context.getString("Id",null);
			WorldExp obj = WorldExp.fetch(con,id);
			context.setValue("WorldExp",obj);
			context.setNextUrl(path+"WorldExpView.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("deleteWorldExp"))
		{
			context.setErrorUrl(path+"WorldExpList.jsp");
			deleteWorldExp(con,context);
			backWorldExp(con,context,path);
			return;
		}
			
		if(action_code.equalsIgnoreCase("toAddWorldExp"))
		{
			WorldExp obj = WorldExp.fetch();
			WorldExpPageData data = (WorldExpPageData)PageData.fromTable(obj,WorldExpPageData.class);
			context.setValue("WorldExpPageData",data);
			context.setNextUrl(path+"WorldExpEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toUpdateWorldExp"))
		{
			String id = context.getString("Id",null);
			WorldExp obj = WorldExp.fetch(con,id);
			WorldExpPageData data = (WorldExpPageData)PageData.fromTable(obj,WorldExpPageData.class);
			context.setValue("WorldExpPageData",data);
			context.setNextUrl(path+"WorldExpEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toCopyWorldExp"))
		{
			String id = context.getString("Id",null);
			WorldExp obj = WorldExp.copy(WorldExp.fetch(con,id));
			WorldExpPageData data = (WorldExpPageData)PageData.fromTable(obj,WorldExpPageData.class);
			context.setValue("WorldExpPageData",data);
			context.setNextUrl(path+"WorldExpEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("saveWorldExp"))
		{
			context.setErrorUrl(path+"WorldExpEdit.jsp");
			saveWorldExp(con,context,loginContext);
			backWorldExp(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("saveAndNewWorldExp"))
		{
			context.setErrorUrl(path+"WorldExpEdit.jsp");
			saveWorldExp(con,context,loginContext);
			WorldExp obj = WorldExp.fetch();
			WorldExpPageData data = (WorldExpPageData)PageData.fromTable(obj,WorldExpPageData.class);
			context.setValue("WorldExpPageData",data);
			context.setNextUrl(path+"WorldExpEdit.jsp");			
			return;
		}
		if(action_code.equalsIgnoreCase("backListWorldExp"))
		{
			backWorldExp(con,context,path);
			return;
		}
		
		//add by Arrow start

		//add by Arrow end

		Logger.throwException("WorldExpSrv","unknown action_code="+action_code);
	}
}
